/*
 * @Author: tom
 * @Date: 2022-08-29 14:47:48
 * @LastEditors: 阿航
 * @LastEditTime: 2022-11-02 14:13:18
 * @FilePath: \shop\src\router\index.js
 */
import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
import {isLogined} from '../utils/auth'

const routes = [
  {
    name: 'layout',
    path: '/',
    component: () => import('../Layout/index.vue'),
    redirect: 'index',
    children: [
      {
        path: '/index',
        component: () => import('../views/dashboard/index.vue'),
        name: 'Index',
      },

      {
        name: 'banner',
        path: '/banner/index',
        component: () => import('../views/banner/index.vue'),
      },
      {
        name: 'bannerCategories',
        path: '/banner/categories',
        component: () => import('../views/banner/categories.vue'),
      },
      {
        name: 'product',
        path: '/products/index',
        component: () => import('../views/products/index.vue'),
      },
      {
        name: 'productCategories',
        path: '/products/categories',
        component: () => import('../views/products/categories.vue'),
      },
      {
        name: 'user',
        path: '/user/index',
        component: () => import('../views/user/index.vue'),
      },
      {
        name: 'wangEdit',
        path: '/wangEdit',
        component: () => import('../views/wangEdit/index.vue'),
      },
      {
        name: 'info',
        path: '/userInfo',
        component: () => import('../views/userInfo/index.vue'),
      },
    ],
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('../views/login/index.vue'),
    // redirect: 'index',
  },
]

const router = createRouter({
  //history: createWebHashHistory(), // createWebHashHistory 带#号
  history: createWebHistory(), // 这个不带#号
  routes,
})

router.beforeEach((to, form, next) => {
  // console.log(isLogined())
  if (!sessionStorage.getItem('token')) {
    if (to.name == 'login') {
      next()
    } else {
      router.push('login')
    }
  } else {
    next()
  }
})
export default router
